from H3DInterface import *

startAtCoord = 0

''' function to generate a cuboid from triangle set
  param size: actual size of cuboid
  param columns: num of horizontal coords
  param rows: num of vertical coords
  param do_tex_coord: true or false, whether to generate texture coords as well 
  returns an IndexedTriangleSet node set with with coord, index and if param do_tex_coord was true, the texture coordinates '''
def createConstantITSCube( size, columns, rows, do_tex_coord="False" ):
  info = []
  for i in range(6):
    if i%2: info.append(rows)
    else: info.append(columns)
  return createITSCube( size, info, do_tex_coord )


''' function to generate a cuboid from triangle set
  param size: actual size of cuboid
  param coordInfo: a list of 6 integers denoting number of coords for each face of cuboid
          i.e. [ <num of horizontal coords for face XY> , < num vertical coords for face XY>,
                 <num of horizontal coords for face ZY> , < num vertical coords for face ZY>,
                 <num of horizontal coords for face XZ> , < num vertical coords for face XZ>]
  param do_tex_coord: true or false, whether to generate texture coords as well 
  returns an IndexedTriangleSet node set with with coord, index and if param do_tex_coord was true, the texture coordinates'''
def createITSCube( size, coord_info, do_tex_coord="False" ):
  coords = []
  tex_coords = []
  index = []

  # create coordinates and index for sides of cube in plane x-y
  createTrianglesOnCubeFaces( "xy", size, coord_info[0], coord_info[1], do_tex_coord, coords, tex_coords, index )
  # create coordinates and index for sides of cube in plane y-z
  createTrianglesOnCubeFaces( "yz", size, coord_info[2], coord_info[3], do_tex_coord, coords, tex_coords, index )
  # create coordinates and index for sides of cube in plane x-z
  createTrianglesOnCubeFaces( "xz", size, coord_info[4], coord_info[5], do_tex_coord, coords, tex_coords, index )
  
  its = createX3DNodeFromString( "<IndexedTriangleSet solid=\"FALSE\" />" )[0]
  coord = createX3DNodeFromString( "<Coordinate />" )[0]
  coord.point.setValue( coords )
  its.index.setValue( index )
  its.coord.setValue( coord )
  if do_tex_coord:
    tex_coord = createX3DNodeFromString( "<TextureCoordinate />" )[0]
    tex_coord.point.setValue( tex_coords )
    its.texCoord.setValue( tex_coord )
  global startAtCoord
  startAtCoord = 0
  return its


''' function to create vector of coordinates and index for two parallel faces of a cuboid
  param faces: indicates the faces of a cube for coordinates generation i.e "xy", "yz", "xz"
  param SFloat num_col, num_row: number of coordinate points to generate of horizontally and vertically
  param SFVec3f size: actual dimension of cube'''
def createTrianglesOnCubeFaces( faces, size, num_col, num_row, do_tex_coord, coords, tex_coords, index ):
  global startAtCoord

  col_size = size.x
  row_size = size.y
  depth = size.z
  
  if faces == "xz" or faces == "zx":
    col_size = size.x
    row_size = size.z
    depth = size.y
  elif faces == "yz" or faces == "zy":
    col_size = size.z
    row_size = size.y
    depth = size.x	

  step_c = col_size/(num_col - 1)
  step_r = row_size/(num_row - 1)
  if do_tex_coord: 
    tc_step_c = 1.0/(num_col - 1)
    tc_step_r = 1.0/(num_row - 1)

  # create coordinates
  for d in [depth/2, -depth/2]:
    for c in range( num_col ):
      for r in range( num_row ):
        if faces == "xy" or faces == "yx":
          coords.append( Vec3f( step_c * c - col_size/2, step_r * r - row_size/2, d ) )
          if do_tex_coord: tex_coords.append( Vec2f( tc_step_c * c, tc_step_r * r ) )
        elif faces == "xz" or faces == "zx":
          coords.append( Vec3f( step_c * c - col_size/2, d, step_r * r - row_size/2 ) )
          if do_tex_coord: tex_coords.append( Vec2f( tc_step_c * c, tc_step_r * r ) )
        elif faces == "yz" or faces == "zy":
          coords.append( Vec3f( d, step_r * r - row_size/2, step_c * c - col_size/2 ) )
          if do_tex_coord: tex_coords.append( Vec2f( tc_step_c * c, tc_step_r * r ) )

  # create index
  for i in [startAtCoord, startAtCoord + num_col*num_row]: 
    for c in range( num_col - 1  ):
      for r in range( num_row - 1 ):
        v0 = i + c * num_row + r
        v1 = i + c * num_row + r+1
        v2 = i + (c+1) * num_row + r+1
        v3 = i + (c+1) * num_row + r
        index.extend([v0, v1, v2, v0, v2, v3 ])

  startAtCoord = startAtCoord + 2*(num_col)*(num_row)
